package com.yinzifan.manager.rpc;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl;
import com.yinzifan.api.ProductRpc;
import com.yinzifan.api.domain.ProductRpcReq;
import com.yinzifan.entity.Product;
import com.yinzifan.manager.service.ProductService;

@AutoJsonRpcServiceImpl
@Service
public class ProductRpcImpl implements ProductRpc {

	private static final Logger LOGGER = LoggerFactory.getLogger(ProductRpcImpl.class);

	@Autowired
	private ProductService productService;

	@Override
	public List<Product> query(ProductRpcReq req) {
		LOGGER.debug("查询多个产品, 参数: {}", req.toString());
		Pageable pageable = PageRequest.of(0, 1000, Sort.Direction.DESC, "rewardRate");
		Page<Product> result = productService.query(req.getIdList(), req.getMinRewardRate(), req.getMaxRewardRate(),
				req.getStatusList(), pageable);
		LOGGER.debug("查询多个产品, 结果: {}", result);
		return result.getContent();
	}

	@Override
	public Product findOne(String id) {
		LOGGER.debug("查询产品详情, 参数: {}", id);
		Product result = productService.findOne(id);
		LOGGER.debug("查询产品详情, 结果: {}", result);
		return result;
	}

}
